{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "file1 = open('../../datasets/Amazon-531/gpt_result/find_dominant_new.txt', 'r')\n",
    "documents = file1.readlines()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'le'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "documents[24][8:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len('ANSWER: <<ANSWER>> ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "241"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "count = 0\n",
    "for result in documents:\n",
    "    if result[8:10] == 'NO':\n",
    "        count += 1\n",
    "count"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "AAPD: 1- 80/1000 = 92%\n",
    "RCV1: 1 - 83/1000 = 91.7%\n",
    "DBPedia-298: 95.2%\n",
    "Reuters: 93.8%"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "AAPD:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.952"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 - 48/1000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['economic performance; economics; equity markets; bond markets; markets; \\n',\n",
       " 'capacity/facilities; corporate/industrial; \\n',\n",
       " 'accounts/earnings; performance; corporate/industrial; employment/labour; economics; government/social; labour issues; \\n',\n",
       " 'accounts/earnings; performance; corporate/industrial; \\n',\n",
       " 'strategy/plans; new products/services; corporate/industrial; \\n']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "file1 = open('../../datasets/RCV1/train_label.txt', 'r')\n",
    "raw_label_set = file1.readlines()\n",
    "raw_label_set[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "60000"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(raw_label_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'economic performance': 711,\n",
       " 'economics': 9186,\n",
       " 'equity markets': 3362,\n",
       " 'bond markets': 1725,\n",
       " 'markets': 14727,\n",
       " 'capacity/facilities': 2509,\n",
       " 'corporate/industrial': 27872,\n",
       " 'accounts/earnings': 4935,\n",
       " 'performance': 9715,\n",
       " 'employment/labour': 1229,\n",
       " 'government/social': 18650,\n",
       " 'labour issues': 1265,\n",
       " 'strategy/plans': 1940,\n",
       " 'new products/services': 648,\n",
       " 'commodity markets': 6478,\n",
       " 'forex markets': 1766,\n",
       " 'money markets': 3844,\n",
       " 'consumer finance': 185,\n",
       " 'legal/judicial': 894,\n",
       " 'crime, law enforcement': 2527,\n",
       " 'regulation/policy': 2782,\n",
       " 'markets/marketing': 2987,\n",
       " 'labour': 904,\n",
       " 'monetary/economic': 1759,\n",
       " 'interbank markets': 2193,\n",
       " 'mergers/acquisitions': 3260,\n",
       " 'ownership changes': 3936,\n",
       " 'domestic politics': 4274,\n",
       " 'energy markets': 1667,\n",
       " 'merchandise trade': 1085,\n",
       " 'trade/reserves': 1728,\n",
       " 'european community': 1302,\n",
       " 'ec external relations': 249,\n",
       " 'international relations': 3162,\n",
       " 'elections': 1087,\n",
       " 'comment/forecasts': 5028,\n",
       " 'production/services': 2178,\n",
       " 'metals trading': 834,\n",
       " 'advertising/promotion': 114,\n",
       " 'money supply': 193,\n",
       " 'consumer prices': 528,\n",
       " 'inflation/prices': 602,\n",
       " 'consumer credit': 28,\n",
       " 'contracts/orders': 1266,\n",
       " 'funding/capital': 3299,\n",
       " 'domestic markets': 385,\n",
       " 'external markets': 541,\n",
       " 'expenditure/revenue': 1180,\n",
       " 'government finance': 3373,\n",
       " 'war, civil war': 3125,\n",
       " 'credit ratings': 632,\n",
       " 'government borrowing': 2254,\n",
       " 'arts, culture, entertainment': 321,\n",
       " 'annual results': 859,\n",
       " 'share listings': 515,\n",
       " 'management moves': 836,\n",
       " 'management': 908,\n",
       " 'market share': 90,\n",
       " 'privatisations': 569,\n",
       " 'leading indicators': 448,\n",
       " 'soft commodities': 3686,\n",
       " 'monopolies/competition': 315,\n",
       " 'bonds/debt issues': 796,\n",
       " 'insolvency/liquidity': 130,\n",
       " 'ec monetary/economic': 411,\n",
       " 'ec institutions': 140,\n",
       " 'share capital': 1269,\n",
       " 'ec competition/subsidy': 154,\n",
       " 'ec corporate policy': 152,\n",
       " 'sports': 2364,\n",
       " 'unemployment': 151,\n",
       " 'disasters and accidents': 769,\n",
       " 'defence contracts': 89,\n",
       " 'asset transfers': 346,\n",
       " 'environment and natural world': 522,\n",
       " 'health': 510,\n",
       " 'loans/credits': 194,\n",
       " 'balance of payments': 253,\n",
       " 'retail sales': 96,\n",
       " 'defence': 669,\n",
       " 'weather': 406,\n",
       " 'research/development': 208,\n",
       " 'ec internal market': 169,\n",
       " 'human interest': 198,\n",
       " 'religion': 237,\n",
       " 'welfare, social services': 120,\n",
       " 'travel and tourism': 50,\n",
       " 'biographies, personalities, people': 421,\n",
       " 'reserves': 150,\n",
       " 'wholesale prices': 74,\n",
       " 'science and technology': 152,\n",
       " 'obituaries': 47,\n",
       " 'ec agriculture policy': 206,\n",
       " 'output/capacity': 191,\n",
       " 'industrial production': 134,\n",
       " 'personal income': 32,\n",
       " 'housing starts': 31,\n",
       " 'inventories': 11,\n",
       " 'ec general': 5,\n",
       " 'fashion': 12,\n",
       " 'ec environment issues': 17,\n",
       " 'capacity utilization': 6}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "true_label_array = {}\n",
    "for line in raw_label_set:\n",
    "    label_list = line[:-3].split('; ')\n",
    "    for label in label_list:\n",
    "        if not label in true_label_array:\n",
    "            true_label_array[label] = 1\n",
    "        else:\n",
    "            true_label_array[label] += 1\n",
    "true_label_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('corporate/industrial', 27872),\n",
       " ('government/social', 18650),\n",
       " ('markets', 14727),\n",
       " ('performance', 9715),\n",
       " ('economics', 9186),\n",
       " ('commodity markets', 6478),\n",
       " ('comment/forecasts', 5028),\n",
       " ('accounts/earnings', 4935),\n",
       " ('domestic politics', 4274),\n",
       " ('ownership changes', 3936),\n",
       " ('money markets', 3844),\n",
       " ('soft commodities', 3686),\n",
       " ('government finance', 3373),\n",
       " ('equity markets', 3362),\n",
       " ('funding/capital', 3299),\n",
       " ('mergers/acquisitions', 3260),\n",
       " ('international relations', 3162),\n",
       " ('war, civil war', 3125),\n",
       " ('markets/marketing', 2987),\n",
       " ('regulation/policy', 2782),\n",
       " ('crime, law enforcement', 2527),\n",
       " ('capacity/facilities', 2509),\n",
       " ('sports', 2364),\n",
       " ('government borrowing', 2254),\n",
       " ('interbank markets', 2193),\n",
       " ('production/services', 2178),\n",
       " ('strategy/plans', 1940),\n",
       " ('forex markets', 1766),\n",
       " ('monetary/economic', 1759),\n",
       " ('trade/reserves', 1728),\n",
       " ('bond markets', 1725),\n",
       " ('energy markets', 1667),\n",
       " ('european community', 1302),\n",
       " ('share capital', 1269),\n",
       " ('contracts/orders', 1266),\n",
       " ('labour issues', 1265),\n",
       " ('employment/labour', 1229),\n",
       " ('expenditure/revenue', 1180),\n",
       " ('elections', 1087),\n",
       " ('merchandise trade', 1085),\n",
       " ('management', 908),\n",
       " ('labour', 904),\n",
       " ('legal/judicial', 894),\n",
       " ('annual results', 859),\n",
       " ('management moves', 836),\n",
       " ('metals trading', 834),\n",
       " ('bonds/debt issues', 796),\n",
       " ('disasters and accidents', 769),\n",
       " ('economic performance', 711),\n",
       " ('defence', 669),\n",
       " ('new products/services', 648),\n",
       " ('credit ratings', 632),\n",
       " ('inflation/prices', 602),\n",
       " ('privatisations', 569),\n",
       " ('external markets', 541),\n",
       " ('consumer prices', 528),\n",
       " ('environment and natural world', 522),\n",
       " ('share listings', 515),\n",
       " ('health', 510),\n",
       " ('leading indicators', 448),\n",
       " ('biographies, personalities, people', 421),\n",
       " ('ec monetary/economic', 411),\n",
       " ('weather', 406),\n",
       " ('domestic markets', 385),\n",
       " ('asset transfers', 346),\n",
       " ('arts, culture, entertainment', 321),\n",
       " ('monopolies/competition', 315),\n",
       " ('balance of payments', 253),\n",
       " ('ec external relations', 249),\n",
       " ('religion', 237),\n",
       " ('research/development', 208),\n",
       " ('ec agriculture policy', 206),\n",
       " ('human interest', 198),\n",
       " ('loans/credits', 194),\n",
       " ('money supply', 193),\n",
       " ('output/capacity', 191),\n",
       " ('consumer finance', 185),\n",
       " ('ec internal market', 169),\n",
       " ('ec competition/subsidy', 154),\n",
       " ('ec corporate policy', 152),\n",
       " ('science and technology', 152),\n",
       " ('unemployment', 151),\n",
       " ('reserves', 150),\n",
       " ('ec institutions', 140),\n",
       " ('industrial production', 134),\n",
       " ('insolvency/liquidity', 130),\n",
       " ('welfare, social services', 120),\n",
       " ('advertising/promotion', 114),\n",
       " ('retail sales', 96),\n",
       " ('market share', 90),\n",
       " ('defence contracts', 89),\n",
       " ('wholesale prices', 74),\n",
       " ('travel and tourism', 50),\n",
       " ('obituaries', 47),\n",
       " ('personal income', 32),\n",
       " ('housing starts', 31),\n",
       " ('consumer credit', 28),\n",
       " ('ec environment issues', 17),\n",
       " ('fashion', 12),\n",
       " ('inventories', 11),\n",
       " ('capacity utilization', 6),\n",
       " ('ec general', 5)]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label_sorted_dic = sorted(true_label_array.items(), key=lambda x:x[1], reverse = True)\n",
    "label_sorted_dic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "long_tail_labels = []\n",
    "for key in true_label_array:\n",
    "    if true_label_array[key] < 600:\n",
    "        long_tail_labels.append(key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['consumer finance',\n",
       " 'ec external relations',\n",
       " 'advertising/promotion',\n",
       " 'money supply',\n",
       " 'consumer prices',\n",
       " 'consumer credit',\n",
       " 'domestic markets',\n",
       " 'external markets',\n",
       " 'arts, culture, entertainment',\n",
       " 'share listings',\n",
       " 'market share',\n",
       " 'privatisations',\n",
       " 'leading indicators',\n",
       " 'monopolies/competition',\n",
       " 'insolvency/liquidity',\n",
       " 'ec monetary/economic',\n",
       " 'ec institutions',\n",
       " 'ec competition/subsidy',\n",
       " 'ec corporate policy',\n",
       " 'unemployment',\n",
       " 'defence contracts',\n",
       " 'asset transfers',\n",
       " 'environment and natural world',\n",
       " 'health',\n",
       " 'loans/credits',\n",
       " 'balance of payments',\n",
       " 'retail sales',\n",
       " 'weather',\n",
       " 'research/development',\n",
       " 'ec internal market',\n",
       " 'human interest',\n",
       " 'religion',\n",
       " 'welfare, social services',\n",
       " 'travel and tourism',\n",
       " 'biographies, personalities, people',\n",
       " 'reserves',\n",
       " 'wholesale prices',\n",
       " 'science and technology',\n",
       " 'obituaries',\n",
       " 'ec agriculture policy',\n",
       " 'output/capacity',\n",
       " 'industrial production',\n",
       " 'personal income',\n",
       " 'housing starts',\n",
       " 'inventories',\n",
       " 'ec general',\n",
       " 'fashion',\n",
       " 'ec environment issues',\n",
       " 'capacity utilization']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "long_tail_labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "tail_dic = {}\n",
    "for line in documents:\n",
    "    if line[8:10] != 'NO':\n",
    "        label = line.strip()[8:]\n",
    "        if (label in long_tail_labels) and (not label in tail_dic):\n",
    "            tail_dic[label] = 1\n",
    "        elif (label in long_tail_labels) and ( label in tail_dic):\n",
    "            tail_dic[label] += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'consumer finance': 1,\n",
       " 'advertising/promotion': 4,\n",
       " 'money supply': 3,\n",
       " 'consumer prices': 1,\n",
       " 'arts, culture, entertainment': 2,\n",
       " 'privatisations': 10,\n",
       " 'monopolies/competition': 2,\n",
       " 'external markets': 2,\n",
       " 'market share': 1,\n",
       " 'insolvency/liquidity': 4,\n",
       " 'ec monetary/economic': 1,\n",
       " 'ec competition/subsidy': 2,\n",
       " 'defence contracts': 2,\n",
       " 'loans/credits': 3,\n",
       " 'retail sales': 2,\n",
       " 'health': 7,\n",
       " 'domestic markets': 2,\n",
       " 'weather': 6,\n",
       " 'research/development': 1,\n",
       " 'ec internal market': 1,\n",
       " 'human interest': 1,\n",
       " 'travel and tourism': 1,\n",
       " 'biographies, personalities, people': 2,\n",
       " 'reserves': 2,\n",
       " 'balance of payments': 1}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(tail_dic)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "57"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "total_count = 0\n",
    "true_label_array = {}\n",
    "for line in raw_label_set[:1000]:\n",
    "    label_list = line[:-3].split('; ')\n",
    "    flag = 0\n",
    "    for tail_label in long_tail_labels:\n",
    "        if tail_label in label_list:\n",
    "            flag = 1\n",
    "            break\n",
    "    if flag == 1:\n",
    "        total_count +=1\n",
    "total_count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tail_count = 0\n",
    "for key in tail_dic:\n",
    "    tail_count += tail_dic[key]\n",
    "tail_count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4010416666666667"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(64 + 13) / (135 + 57)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "true_label_array = {}\n",
    "for line in raw_label_set[:1000]:\n",
    "    label_list = line[:-3].split('; ')\n",
    "    for label in label_list:\n",
    "        if label in long_tail_labels:\n",
    "            true_label_array[label] = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'consumer finance': 1,\n",
       " 'ec external relations': 1,\n",
       " 'advertising/promotion': 1,\n",
       " 'money supply': 1,\n",
       " 'consumer prices': 1,\n",
       " 'consumer credit': 1,\n",
       " 'domestic markets': 1,\n",
       " 'external markets': 1,\n",
       " 'arts, culture, entertainment': 1,\n",
       " 'share listings': 1,\n",
       " 'market share': 1,\n",
       " 'privatisations': 1,\n",
       " 'leading indicators': 1,\n",
       " 'monopolies/competition': 1,\n",
       " 'insolvency/liquidity': 1,\n",
       " 'ec monetary/economic': 1,\n",
       " 'ec institutions': 1,\n",
       " 'ec competition/subsidy': 1,\n",
       " 'ec corporate policy': 1,\n",
       " 'unemployment': 1,\n",
       " 'defence contracts': 1,\n",
       " 'asset transfers': 1,\n",
       " 'environment and natural world': 1,\n",
       " 'health': 1,\n",
       " 'loans/credits': 1,\n",
       " 'balance of payments': 1,\n",
       " 'retail sales': 1,\n",
       " 'weather': 1,\n",
       " 'research/development': 1,\n",
       " 'ec internal market': 1,\n",
       " 'human interest': 1,\n",
       " 'religion': 1,\n",
       " 'welfare, social services': 1,\n",
       " 'travel and tourism': 1,\n",
       " 'biographies, personalities, people': 1,\n",
       " 'reserves': 1,\n",
       " 'wholesale prices': 1}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "true_label_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Multi-Label",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
